import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // articles: require('@/data/articles.json'),
    drawer: false,
    refresh: 1,
    items: [
      {
        text: '首页',
        href: '#!',
      },
      {
        text: '关于',
        href: '#about',
      },
    ],
  },
  getters: {
    categories: state => {
      const categories = []

      for (const article of state.articles) {
        if (
          !article.category
          || categories.find(category => category.text === article.category)
        ) continue

        const text = article.category

        categories.push({
          text,
          href: '#!',
        })
      }

      return categories.sort().slice(0, 4)
    },
    links: (state, getters) => state.items.concat(getters.categories),
  },
  mutations: {
    setDrawer: (state, payload) => (state.drawer = payload),
    toggleDrawer: state => (state.drawer = !state.drawer),
    refreshList(state){
        if(state.refresh % 2 === 0){
            state.refresh -= 1
        }
        else{
            state.refresh += 1
        }
    }
  },
  actions: {

  },
})
